package com.kac.service.Impl;

import com.codingapi.txlcn.client.bean.DTXLocal;
import com.codingapi.txlcn.commons.annotation.DTXPropagation;
import com.codingapi.txlcn.commons.annotation.LcnTransaction;
import com.codingapi.txlcn.commons.annotation.TccTransaction;
import com.codingapi.txlcn.commons.annotation.TxcTransaction;
import com.kac.entity.KacUser;
import com.kac.repository.KacUserRepository;
import com.kac.service.KacUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Qilin
 * @date 2019/01/16
 */
@Service
@Slf4j
public class KacUserServiceImpl implements KacUserService {

    @Autowired
    private KacUserRepository kacUserRepository;

    private ConcurrentHashMap<String, Long> ids = new ConcurrentHashMap<>();

    @Override
//    @TccTransaction(dtxp = DTXPropagation.SUPPORTS)
    @TxcTransaction(dtxp = DTXPropagation.SUPPORTS)
    @Transactional
    public void save() {
        KacUser user =  new KacUser();
        user.setName("user");
        int rs1 = kacUserRepository.save(user).getId();

        int v = 100/0;

//        ids.put(DTXLocal.cur().getGroupId(), Long.valueOf(user.getId()));
    }

//    public void confirmSave() {
//        log.info("tcc-confirm-" + DTXLocal.getOrNew().getGroupId());
//        ids.remove(DTXLocal.getOrNew().getGroupId());
//    }
//
//    public void cancelSave() {
//        log.info("tcc-cancel-" + DTXLocal.getOrNew().getGroupId());
//        System.out.println((ids.get(DTXLocal.getOrNew().getGroupId())).intValue());
//        kacUserRepository.deleteById((ids.get(DTXLocal.getOrNew().getGroupId())).intValue());
//    }
}
